package models

import (
	"0519/common/initialize"
	course "0519/common/proto"
	"github.com/google/uuid"
	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	Mobile   string  `gorm:"type:char(11);not null;"`
	Name     string  `gorm:"type:varchar(20);not null;"`
	Email    string  `gorm:"type:varchar(20);"`
	Password string  `gorm:"type:varchar(32);not null;"`
	Level    int     `gorm:"type:int(10);not null;commit:'等级 0: 用户 1: 管理员'"`
	Balance  float64 `gorm:"type:decimal(10,2);not null;commit:'余额'"`
}

func (u User) GetUserMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Model(u).Where("mobile=?", mobile).First(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

func (u User) GetUserId(id int64) (user *User, err error) {
	err = initialize.DB.Model(u).Where("id=?", id).First(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

// 课程表
type Course struct {
	gorm.Model
	Name              string  `gorm:"type:varchar(20);not null;"`
	CourseInformation string  `gorm:"type:varchar(50);not null;commit:'课程信息'"`
	CourseDuration    string  `gorm:"type:varchar(50);not null;commit:'课程时长'"`
	Price             float64 `gorm:"type:decimal(10,2);not null;"`
	Stock             int     `gorm:"type:int(10);not null;commit:'库存'"`
	State             int     `gorm:"type:int(10);not null;commit:'状态 1:上架 2:下架'"`
	Picture           string  `gorm:"type:varchar(255);not null;commit:'课程图片'"`
}

func (c Course) CourseAdd(in *course.CourseAddReq) (course *Course, err error) {
	course = &Course{
		Name:              in.Name,
		CourseInformation: in.CourseInformation,
		CourseDuration:    in.CourseDuration,
		Price:             float64(in.Price),
		Stock:             int(in.Stock),
		State:             int(in.State),
		Picture:           in.Picture,
	}
	err = initialize.DB.Create(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c Course) GetCourse() (course []*Course, err error) {
	err = initialize.DB.Find(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c Course) GetCourseId(id int64) (course []*Course, err error) {
	err = initialize.DB.Where("id=?", id).First(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c Course) GetCourseIds(id int64) (course *Course, err error) {
	err = initialize.DB.Where("id=?", id).First(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c Course) CourseUpdateState(in *course.CourseUpdateStateReq) error {
	err := initialize.DB.Model(c).Where("id=?", in.CourseId).Update("state", in.State).Error
	if err != nil {
		return err
	}
	return nil
}

func (c Course) CourseDeleted(id int64) error {
	err := initialize.DB.Model(c).Where("id=?", id).Delete("state").Error
	if err != nil {
		return err
	}
	return nil
}

// 限时优惠课程表
type DiscountCourse struct {
	gorm.Model
	CourseId  int     `gorm:"type:int(10);not null;commit:'课程Id'"`
	StartTime string  `gorm:"type:datetime;not null;commit:'开始时间'"`
	EndTime   string  `gorm:"type:datetime;not null;commit:'结束时间'"`
	Price     float64 `gorm:"type:decimal(10,2);not null;commit:'原价格'"`
	NowPrice  float64 `gorm:"type:decimal(10,2);not null;commit:'现价格'"`
	Num       int     `gorm:"type:int(10);not null;commit:'数量'"`
}

func (d DiscountCourse) CreateDiscountCourse(in *course.CreateDiscountCourseReq, price float64, num int) (course *DiscountCourse, err error) {
	course = &DiscountCourse{
		CourseId:  int(in.CourseId),
		StartTime: in.StartTime,
		EndTime:   in.EndTime,
		Price:     price,
		NowPrice:  float64(in.NowPrice),
		Num:       int(in.Num),
	}
	tx := initialize.DB.Begin()
	// 注意，一旦你在一个事务中，使用tx作为数据库句柄
	if err = tx.Create(&course).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Model(&Course{}).Where("id = ?", in.CourseId).Update("stock", num).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	tx.Commit()
	return
}

func (d DiscountCourse) GetDiscountCourseId(id int64) (course *DiscountCourse, err error) {
	err = initialize.DB.Where("id=?", id).First(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

// 课程订单表
type OrderCourse struct {
	gorm.Model
	CourseId int     `gorm:"type:int(100);not null;commit:'课程Id'"`
	UserId   int     `gorm:"type:int(100);not null;commit:'用户Id'"`
	OrderSn  string  `gorm:"type:varchar(50);not null;commit:'订单编号'"`
	Num      int     `gorm:"type:int(100);not null;commit:'数量'"`
	Price    float64 `gorm:"type:decimal(10,2);not null;commit:'价格'"`
	Status   int     `gorm:"type:int(100);not null;commit:'订单状态'"`
	Pay      int     `gorm:"type:int(100);not null;commit:'支付方式'"`
}

func (c OrderCourse) CreateDiscountCourseOrder(in *course.CreateDiscountCourseOrderReq, num int, price float64) (order *OrderCourse, err error) {
	OrderSn := uuid.New().String()
	order = &OrderCourse{
		CourseId: int(in.CourseId),
		UserId:   int(in.UserId),
		OrderSn:  OrderSn,
		Num:      int(in.Num),
		Price:    float64(in.Num) * price,
		Status:   0,
		Pay:      1,
	}
	tx := initialize.DB.Begin()
	// 注意，一旦你在一个事务中，使用tx作为数据库句柄
	if err = tx.Create(&order).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	if err = tx.Model(&DiscountCourse{}).Where("id = ?", in.CourseId).Update("num", num).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	tx.Commit()
	return
}

func (c OrderCourse) OrderList() (order []*OrderCourse, err error) {
	err = initialize.DB.Find(&order).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c OrderCourse) OrderId(in *course.OrderPayReq) (order *OrderCourse, err error) {
	err = initialize.DB.Where("id=?", in.OrderId).First(&order).Error
	if err != nil {
		return nil, err
	}
	return
}
